package com.yanu.trace.t2s.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yanu.trace.t2s.entity.Enterprise;
import com.yanu.trace.t2s.entity.Pig;
import com.yanu.trace.t2s.service.EnterpriseService;
import com.yanu.trace.t2s.service.PigService;
import com.yanu.trace.t2s.service.PorkService;
import com.yanu.trace.t2s.util.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author whj
 * @since 2021-06-23
 */
@RestController
@RequestMapping("/pig")
public class PigController {
    @Autowired
    PigService pigService;
    @Autowired
    PorkService porkService;
    @Autowired
    EnterpriseService enterpriseService;

    /**
     * 加入新猪或者修改猪的信息
     * @param s
     * @return
     */
    @PostMapping("/addorupdatepig")
    public Message addorupdatepig(@RequestBody String s) {
        Message msg = new Message();
        Pig pig = JSON.parseObject(s, Pig.class);
        if (pigService.saveOrUpdate(pig)) {
            msg.setSuccess(pig);
        } else {
            msg.setFail(410);
        }
        return msg;
    }

    /**
     * 传入页数大小和企业id来分页查询猪的信息
     * @param s
     * @return
     */
    @PostMapping("/listbyeid")
    public Message listbyeid(@RequestBody String s) {
        Message msg = new Message();
        JSONObject jsonObject = JSONObject.parseObject(s);
        Enterprise enterprise = enterpriseService.getById(jsonObject.getInteger("enter_id"));
        IPage<Pig> pigIPage = new Page<>();
        if (enterprise.getType() != 0) {
            pigIPage = pigService.selectPage(new Page<>(jsonObject.getLong("current"),
                    jsonObject.getLong("size")), jsonObject.getInteger("enter_id"));//根据eid和页面的信息进行查询企业所有的猪
        } else {
            pigIPage = pigService.selectPageAll(new Page<>(jsonObject.getLong("current"),//超级管理员查看所有
                    jsonObject.getLong("size")));
        }
        if (pigIPage != null) {
            msg.setSuccess(pigIPage);
        } else {
            msg.setFail(409);
        }
        return msg;
    }

    /**
     * 根据猪id获取猪的信息
     * @param s
     * @return
     */
    @PostMapping("/getPigByID")
    public Message getPigByID(@RequestBody String s) {
        Message msg = new Message();
        JSONObject jsonObject = JSONObject.parseObject(s);
        Pig pig = pigService.getById(jsonObject.getString("id"));//根据猪id获取猪的信息
        if(pig!=null){
            msg.setSuccess(pig);
        }else {
            msg.setCode(411);
            msg.setMsg("未查询到此id");
        }
        return msg;
    }

    /**
     * 根据id删除
     * @param info
     * @return
     */
    @PostMapping("/deleteByID")
    public Message deleteByID(@RequestBody String info) {
        Message msg = new Message();
        JSONObject jsonObject = JSONObject.parseObject(info);
        JSONArray array = jsonObject.getJSONArray("id");//获取id数组
        for (int i = 0; i < array.size(); i++) {
            if (!pigService.removeById(array.getInteger(i)))//删除猪实体
                msg.setFail(416);
        }
        msg.setSuccess(null);
        return msg;
    }

    /**
     * 检疫猪 可以批量通过
     * @param info
     * @return
     */
    @PostMapping("/examine")
    public Message examineEnter(@RequestBody String info) {
        Message msg = new Message();
        JSONObject jsonObject = JSONObject.parseObject(info);//实例化json对象
        JSONArray array = jsonObject.getJSONArray("id");//获取猪的id数组
        for (int i = 0; i < array.size(); i++) {
            UpdateWrapper<Pig> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", array.getInteger(i)).set("check_status", jsonObject.getInteger("type"));//根据id查询猪来设置猪的检疫状态
            if (!pigService.update(updateWrapper)) {
                msg.setFail(412);
                return msg;
            }
        }
        msg.setSuccess(null);
        return msg;
    }
}

